package test.util

import io.github.frailty9.crserver.App
import io.github.frailty9.crserver.utils.IdGeneratorFactory
import io.github.frailty9.crserver.utils.SnowflakeIdWorker
import org.junit.jupiter.api.Test
import org.noear.solon.annotation.Inject
import org.noear.solon.test.SolonTest

@SolonTest(App::class)
class IdTest(
    private val idGenerator: IdGeneratorFactory
) {

    @Test
    fun testGenerateId() {
        val id = idGenerator.nextId()
        println(id)
    }

    @Test
    fun testParseTimestamp() {
        val snowflakeIdWorker = SnowflakeIdWorker(0, 0)
        val timestamp = System.currentTimeMillis()
        val id = snowflakeIdWorker.nextId(timestamp)
        assert(snowflakeIdWorker.parseTimestamp(id) == timestamp)
    }

    @Test
    fun testParseWorkerId() {
        val workerId = 11L
        val snowflakeIdWorker = SnowflakeIdWorker(workerId, 0)
        val id = snowflakeIdWorker.nextId()
        assert(snowflakeIdWorker.parseWorkerId(id) == workerId)
    }

    @Test
    fun testParseDatacenterId() {
        val datacenterId = 22L
        val snowflakeIdWorker = SnowflakeIdWorker(0, datacenterId)
        val id = snowflakeIdWorker.nextId()
        assert(snowflakeIdWorker.parseDatacenterId(id) == datacenterId)
    }

    @Test
    fun testParseSequence() {
        val snowflakeIdWorker = SnowflakeIdWorker(0, 0)
        val id = snowflakeIdWorker.nextId()
        assert(snowflakeIdWorker.parseSequence(id) == 0L)
    }

}